<template>
  <el-form ref="deptForm" :model="form" :rules="formRules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in list" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployeesSimple } from '@/api/employees'
import { addDepartment, editDepartment, getDepartmentDetails } from '@/api/department'
import item from '@/layout/components/Sidebar/Item'
export default {
  props: {
    pid: {
      type: String,
      required: true
    },
    addOrEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    const validCode = (rule, value, callback) => {
      // 验证code是够存在相同的
      let codeList = this.originList.map(item => item.code)
      // console.log(codeList)
      // console.log(this.pid)
      // 如果是编辑页面 需要验证除本身的code码其他code吗中是否有相同
      if (this.addOrEdit) {
        // 先排除自身
        codeList = this.originList.filter(item => item.id !== this.pid).map(item => item.code)
      }
      if (codeList.includes(value)) {
        callback(new Error(`部门编码${value}不能相同`))
      } else {
        callback()
      }
    }
    const validName = (rule, value, callback) => {
      // console.log(this.originList)
      // console.log(this.pid)
      // 验证部门名称是否同级部门存在相同的
      let nameList = this.originList.filter(item => item.pid === this.pid).map(item => item.name)
      // console.log(nameList)
      // 当编辑时 排除除自身的部门名称
      if (this.addOrEdit) {
        const pid = this.originList.find(item => item.id === this.pid).pid
        // console.log(pid)
        nameList = this.originList.filter(item => item.pid === pid).filter(item => item.id !== this.pid).map(item => item.name)
        // console.log(nameList)
      }
      if (nameList.includes(value)) {
        callback(new Error(`部门名称${value}不能相同`))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      formRules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '输入的内容必须在1到50个字符之间', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '输入的内容必须在1到50个字符之间', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '输入的内容必须在1到300个字符之间', trigger: 'blur' }
        ]
      },
      // 负责人信息
      list: []
    }
  },
  created() {
    this.loadEmployeesSimple()
    if (this.addOrEdit) {
      // 编辑时才触发渲染函数
      this.loadDepartmentInfo()
    }
  },
  methods: {
    // 重置表单
    resetForm() {
      this.$refs.deptForm.resetFields()
    },
    hSubmit() {
      // 确定 兜底校验
      this.$refs.deptForm.validate((valid) => {
        if (!valid) return
        // 校验通过
        // 三元运算符 为true表示编辑功能 触发编辑方法 为false为新增触发新增方法
        this.addOrEdit ? this.editDepartmentInfo() : this.addDepartmentInfo()
      })
    },
    // 关闭对话框
    hCancel() {
      this.$emit('closeDialog')
    },
    // 获取负责人信息
    async loadEmployeesSimple() {
      try {
        const { data: res } = await getEmployeesSimple()
        // console.log(res)
        this.list = res
      } catch (error) {
        return new Error(error)
      }
    },
    // 获取部门详情
    async loadDepartmentInfo() {
      try {
        const { data: res } = await getDepartmentDetails(this.pid)
        // console.log(res)
        this.form = res
      } catch (e) {
        console.log(e)
      }
    },
    // 新增部门
    async addDepartmentInfo() {
      try {
        await addDepartment({ pid: this.pid, ...this.form })
        // console.log(res)
        // 关闭弹窗并渲染页面
        this.$emit('depSuccess')
      } catch (e) {
        console.log(e)
      }
    },
    // 编辑部门
    async editDepartmentInfo() {
      try {
        await editDepartment(this.form)
        // console.log(res)
        // 关闭弹窗并渲染页面
        this.$emit('depSuccess')
      } catch (e) {
        console.log(e)
      }
    }
  }
}
</script>
